#include <QByteArray>
#include <QXmlAttributes>
#include <QXmlStreamReader>
#include <QString>
#include <QDebug>
#include <QDateTime>

#include "_Interface/_global_datastruct.h"
#include "Backend/wyzkr688rfswbackend.h"
#include "Package/simpletcpserverdll_global.h"
#include "Package/simpletcpsocketdll_global.h"


wyzkr688rfswbackend::wyzkr688rfswbackend(){
    if(this->pgtrobotcomfsw == nullptr) pgtrobotcomfsw = new gtrobotcomfsw();
    if(this->pBasicRFSWTech == nullptr) pBasicRFSWTech = new BasicRFSWTech();

    this->mDataSaveParam.SaveFlagLast = false;
}
wyzkr688rfswbackend::~wyzkr688rfswbackend(){
    if(this->pgtrobotcomfsw != nullptr) delete this->pgtrobotcomfsw;
    if(this->pBasicRFSWTech != nullptr) delete this->pBasicRFSWTech;
}
short wyzkr688rfswbackend::toRobotTcpServerAdd(){
    return this->pgtrobotcomfsw->toRobotTcpServerAdd();
}
short wyzkr688rfswbackend::toRobotTcpServerListen(){
    return this->pgtrobotcomfsw->toRobotTcpServerListen();
}
bool  wyzkr688rfswbackend::getRobotConnectStatus(){
    return this->pgtrobotcomfsw->getRobotConnectStatus();
}
short wyzkr688rfswbackend::ReadRobotJointData(double (&data)[6]){
    return this->pgtrobotcomfsw->ReadRobotJointData(data);
}
short wyzkr688rfswbackend::ReadRobotJointTorData(double (&data)[6]){
    return this->pgtrobotcomfsw->ReadRobotJointTorData(data);
}
short wyzkr688rfswbackend::ReadSpindleSpeed(double& data){
    return this->pgtrobotcomfsw->ReadSpindleSpeed(data);
}
short wyzkr688rfswbackend::ReadForceControlStep(double& mstep){
    return this->pgtrobotcomfsw->ReadForceControlStep(mstep);
}
short wyzkr688rfswbackend::ReadForceControlForceSetValue(double &data){
    return this->pgtrobotcomfsw->ReadForceControlForceSetValue(data);
}
short wyzkr688rfswbackend::ReadForceControlForceRealValue(double &Data){
    return this->pgtrobotcomfsw->ReadForceControlForceRealValue(Data);
}
short wyzkr688rfswbackend::RFSWPorcess(){

    return 1;
}
short  wyzkr688rfswbackend::toRobotLoopRun(){
    return this->pgtrobotcomfsw->toRobotLoopRun();
}

short wyzkr688rfswbackend::ProcessDataSave(){
    bool AllowNewDataSave = false;

    if((this->mDataSaveParam.SaveFlagLast == false)){
        static QString FileSaveMessage;

        QDateTime current_date_time = QDateTime::currentDateTime();
        QString current_time = current_date_time.toString("hh-mm-ss");
        QString current_date = current_date_time.toString("yyyy-MM-dd-");
        QString filename_data = "../" + current_date + current_time + "-data.csv";

        mDataSaveParam.mSaveFile = new QFile(filename_data);
        mDataSaveParam.mSaveFile->open(QIODevice::WriteOnly | QIODevice::Text);
        mDataSaveParam.mStreamData = new QTextStream(mDataSaveParam.mSaveFile);

        *(mDataSaveParam.mStreamData) << "Time" << ","
            << "FzInit"  << ","
            << "FzSet"   << ","
            << "Fz"      <<","
            << "Spindle" << ","
            << "OVN" << ","
            << "J1"  << ","
            << "J2"  << ","
            << "J3"  << ","
            << "J4"  << ","
            << "J5"  << ","
            << "J6"  << ","
            << "J1T" << ","
            << "J2T" << ","
            << "J3T" << ","
            << "J4T" << ","
            << "J5T" << ","
            << "J6T" << ","
            << "OffsetY" << ","
            << "OffsetZ" << "\n";
        mDataSaveParam.TimeOrderLast = QDateTime::currentDateTime().toMSecsSinceEpoch();
        FileSaveMessage = "&nbsp;&nbsp;&nbsp;&nbsp;" + filename_data;
        mDataSaveParam.SaveFlagLast = true;
    }
    else if((this->mDataSaveParam.SaveFlagLast == true)){
        if(AllowNewDataSave == true){
            double ForceInitSet = 0;
            double ForceSet = 0;
            double ForceReal = 0;
            double Spindle = 0;

            this->pgtrobotcomfsw->ReadForceControlForceInitSetValue(ForceInitSet);
            this->pgtrobotcomfsw->ReadForceControlForceSetValue(ForceSet);
            this->pgtrobotcomfsw->ReadForceControlForceRealValue(ForceReal);
            this->pgtrobotcomfsw->ReadSpindleSpeed(Spindle);

            mDataSaveParam.TimeOrder = QDateTime::currentDateTime().toMSecsSinceEpoch();
            mDataSaveParam.TimeOrderNow = mDataSaveParam.TimeOrder - mDataSaveParam.TimeOrderLast;
            *(mDataSaveParam.mStreamData) << mDataSaveParam.TimeOrderNow << ","
                <<ForceInitSet << ","
                <<ForceSet     << ","
                <<ForceReal    << ","
                <<Spindle <<","
                <<this->pgtrobotcomfsw->mRobotAdjData.toRobotOvn     <<","
                <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointData[0] <<","
                <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointData[1] <<","
                <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointData[2] <<","
                <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointData[3] <<","
                <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointData[4] <<","
                <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointData[5] <<","
                <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointTorData[0]  <<","
                <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointTorData[1]  <<","
                <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointTorData[2]  <<","
                <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointTorData[3]  <<","
                <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointTorData[4]  <<","
                <<this->pgtrobotcomfsw->mRobotDAQData.RobotJointTorData[5]  <<","
                <<this->pgtrobotcomfsw->mRobotDAQData.RobotTTSOffsetData[0] <<","
                <<this->pgtrobotcomfsw->mRobotDAQData.RobotTTSOffsetData[1] << "\n";
            this->mDataSaveParam.TimeOrderLast = mDataSaveParam.TimeOrder;
        }
    }
    else if((this->mDataSaveParam.SaveFlagLast == true)){
        mDataSaveParam.mSaveFile->close();
        delete mDataSaveParam.mSaveFile;
        delete mDataSaveParam.mStreamData;
        this->mDataSaveParam.SaveFlagLast = false;
    }
    else{
    }
    return 1;
}
